﻿/*
	This file is part of Limpet.

    Limpet is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Limpet is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Limpet.  If not, see http://www.gnu.org/licenses/.
	
	Copyright 2010 Dan Popick
*/
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace Open.Web.Diagnostics.Limpet
{
    /// <summary>
    /// utility for dealing with IP addresses
    /// </summary>
    public static class IPUtil
    {
        /// <summary>
        /// used when a netmask isn't provided
        /// </summary>
        static readonly byte[] _zeroBytes;

        static IPUtil()
        {
            _zeroBytes = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        }

        /// <summary>
        /// Check if an IP address matches an address/mask pair
        /// </summary>
        /// <param name="addr">The address to match</param>
        /// <param name="subnet">The subnet IP</param>
        /// <param name="subnetMask">The subnet mask</param>
        /// <returns>True if the subnet address matches.</returns>
        /// <remarks>
        /// Since I don't run an IPv6 network, I have no idea if this really works with IPv6.
        /// </remarks>
        public static bool SubnetMatches(IPAddress addr, IPAddress subnet, IPAddress subnetMask)
        {
            byte[] addrBytes = addr.GetAddressBytes();
            byte[] subnetBytes = subnet.GetAddressBytes();
            byte[] subnetMaskBytes = subnetMask == null ? _zeroBytes : subnetMask.GetAddressBytes();

            if (addrBytes.Length != subnetBytes.Length
                || (subnetMaskBytes != _zeroBytes && subnetBytes.Length != subnetMaskBytes.Length))
                return false;
            
            for (int i = 0; i < subnetBytes.Length; ++i)
                if ((addrBytes[i] & subnetMaskBytes[i]) != (subnetBytes[i] & subnetMaskBytes[i]))
                    return false;

            return true;
        }

        /// <summary>
        /// Determines whether an address matches any elements in a list of subnet/masks
        /// </summary>
        /// <param name="addr">The address to match</param>
        /// <param name="ipList">The IPRestriction list to search</param>
        /// <returns>True if the address matches any of the elements in the list</returns>
        public static bool FindSubnetMatch(IPAddress addr, IList<IPRestriction> ipList)
        {
            bool found = false;
            foreach (IPRestriction ip in ipList)
            {
                if (IPUtil.SubnetMatches(addr, ip.Address, ip.Mask))
                {
                    found = true;
                    break;
                }
            }
            return found;
        }
    }
}
